<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-04-13
 * Time: 2:04
 */

namespace app\api\controller\seller;
use app\common\logic\GoodsLogic;
use app\common\controller\Api;
use app\common\model\Goods;


class Mall extends Api
{
//    protected $authenticate = ['only'=>'ff'];
    
    public function index(){
        $params = paramForm();
        $logic = new GoodsLogic();
        if (isset($params['action'])){
            switch ($params['action']){
                case 'up':
                    $rt = $logic->changeGoodsStatus(paramForm('id',true),3);
                    if($rt){
                        success(null);
                    }else{
                        error('操作失败');
                    }
                    break;
                case 'down':
                    $rt = $logic->changeGoodsStatus(paramForm('id',true),0);
                    if($rt){
                        success(null);
                    }else{
                        error('操作失败');
                    }
                    break;
                default:
                    break;
            }
            die;
        }
        
        if(isset($params['id'])){
            $this->read($params);
        }
       
        $logic->setValue('size',20);
        $logic->setValue('paginate',1);
        $params['user_id'] = $this->getUserId();
        $list = $logic->getList($params);
        success($list);
    }
    
   
    
    
    public function read($params){
    
        $logic = new GoodsLogic();
        $logic->setSql($params);
        $goods = $logic->findModel();
        //简单模式的信息
        if(isset($params['easy'])&&$params['easy']){
            success($goods);
        }
        $goods->attr;
        $goods->city;

        
        $groups = $logic->getGroups($goods->id);
    
        //偷天换日，用团购换正式价格
        if($groups){
        
            $tempArr = [];
            $mallLogic = new MallLogic();
            $goods->stockTotal = 0;
            $groups->info = $groups->getInfo();//排好序的
            foreach ($groups->info as $info){
            
                $mall = $mallLogic->findModel($info->mall_id);
                $mall->specs;
                //替换掉库存和价格。。。
                $mall->price = $info->price;
                $mall->stock = $info->stock;
            
                $tempArr[] = $mall;
            
                $goods->stockTotal+=$mall->stock;
            }
            unset($groups->info);
        
            $goods->mall = $tempArr;
        
        }else{
            $goods->stockTotal = 0;
            $goods->mall = $goods->getMall();//排好序的
            foreach ($goods->mall as $mall){
                $mall->specs;
                $goods->stockTotal+=$mall->stock;
            }
        }
    
        $goods->group = $groups;
    
        $specAllArr = [];
        foreach ($goods->spec as $spec){
            $specAllArr[$spec->spec_id][] = ['spec_id'=>$spec->spec_id,'val'=>$spec->val,'title'=>$spec->title];
        }
    
        unset($goods->spec);
    
        
    
        if($goods){
            success($goods);
        }else{
            error('数据不存在或者无权限查看');
        }
        
        
    }
    

    
}